
package com.zb.controller;

import com.zb.client.SearchFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Controller
public class SearchWebController {

    @Autowired
    private SearchFeignClient searchFeignClient;


    @GetMapping("/search")
    public String search(@RequestParam Map<String, String> param, Model model) {
        System.out.println(param);
        try {
            if (param.get("index") == null) {
                param.put("index", "1");
            }
            param.put("size", "20");
            Map<String, Object> search = searchFeignClient.search(param);
            System.out.println(search);
            model.addAttribute("data", search);
            model.addAttribute("param", param);
            String myurl = this.myurl(param);
            model.addAttribute("myurl", myurl);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "search";
    }

    public String myurl(Map<String, String> param) {
        String url = "/search";
        if (param != null && param.size() > 0) {
            url += "?";
            for (Map.Entry<String, String> entry : param.entrySet()) {
                String key = entry.getKey();
                if (key.equals("sortRule") || key.equals("sortFeild")){
                    continue;
                }
                String value = entry.getValue();
                url += key + "=" + value + "&";
            }
            url = url.substring(0, url.length() - 1);
        }
        System.out.println(url);
        return url;
    }
}
